import { _decorator, Collider2D, Component, Contact2DType, IPhysics2DContact } from 'cc';
import { ItemData } from './ItemData';
import { InventoryManager } from './InventoryManager';
const { ccclass, property } = _decorator;

@ccclass('ItemPickup')
export class ItemPickup extends Component {

    private itemData: ItemData

    start() {
        // 注册单个碰撞体的回调函数
        let collider = this.getComponent(Collider2D);
        if (collider) {
            collider.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
            collider.on(Contact2DType.END_CONTACT, this.onEndContact, this);
        }
    }

    onBeginContact(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
        // 只在两个碰撞体开始接触时被调用一次
        console.log('onBeginContact');
        if (otherCollider.name == 'Player') {
            // 添加到物品到背包
            InventoryManager.Instance.inventoryData.AddItem(this.itemData, this.itemData.amount)
            InventoryManager.Instance.inventoryUI.RefreshUI()
            // 销毁武器
            otherCollider.node.destroy()
        }
    }

    onEndContact(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
        // 只在两个碰撞体结束接触时被调用一次
        console.log('onEndContact');
    }

}